package com.hsy.directseeding.my;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.hsy.directseeding.R;
import com.hsy.directseeding.account.LoginActivity;
import com.hsy.directseeding.common.CommonBaseActivity;
import com.hsy.directseeding.common.Consts;
import com.hsy.directseeding.utils.CommonUtils;
import com.hsy.directseeding.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/9.
 */
public class MyActivity extends CommonBaseActivity implements View.OnClickListener {
    protected ImageView headImage;
    protected TextView changePassword;
    protected TextView logOut, name;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            MyGson.Result result = DPGsonUtil.fromJson(response, MyGson.Result.class);

            DPPicassoUtil.with(MyActivity.this).load(result.getC_logo()).error(R.mipmap.default_head).into(headImage);
            name.setText(result.getName());
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        CommonUtils.showProgressDialog(this);
        Map<String, String> map = new HashMap<String, String>();
        map.put("token", CommonUtils.getToken(this));
        HttpUtil.httpPostWithParams(this, 0, Consts.Apis.POST_MY, map, callBack);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("我的");
        super.setContentView(R.layout.activity_my);
        initView();
        changePassword.setOnClickListener(this);
        logOut.setOnClickListener(this);
    }

    private void initView() {
        headImage = (ImageView) findViewById(R.id.head_image);
        changePassword = (TextView) findViewById(R.id.change_password);
        logOut = (TextView) findViewById(R.id.log_out);
        name = F(R.id.name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_password:
                startActivity(ChangePasswordActivity.class);
                break;
            case R.id.log_out:
                AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("确定退出吗?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        CommonUtils.clearToken(MyActivity.this);
                        startActivityWithTaskCleared(LoginActivity.class);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.show();

                break;
            default:
                break;
        }
    }
}
